package com.lxhll.personnel.config;

import com.lxhll.personnel.core.H;
import com.lxhll.personnel.interceptor.JwtInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;


/**
 * 资源访问管理 WebMvcConfigurer
 * */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Autowired
    private JwtInterceptor jwtInterceptor;
    @Value("${file.image.path}")
    private String ResourceLocations;
    @Autowired
    private H resolver;


    /**
     * 解析器  参数解析器
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(resolver);
    }

    /**
     * jwt拦截器，后台登陆和权限判断，拦截器，所拦截的路径
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //对登录请求放行
        registry.addInterceptor(jwtInterceptor).addPathPatterns("/**")
                .excludePathPatterns("/login")
                .excludePathPatterns("/image/photo/**")
                .excludePathPatterns("/upload")
                .excludePathPatterns("/doc.html/**","/swagger-resources/**","/v2/**","/swagger-ui.html/**","/webjars/**");
    }


    /**
     * 打开跨域
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("GET","HEAD","POST","PUT","DELETE","OPTIONS")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
    }

    // 静态资源路径映射
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 将所有/static/** 访问都映射到classpath:/static/ 目录下
        registry.addResourceHandler("/image/photo/**").addResourceLocations("file:"+ResourceLocations);
    }
}